﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using MisDLL;
using System.Data;

namespace MisBLL
{
    public class DepartDAO : MarshalByRefObject, IDepart
    {
        protected DbHelper dbHelper = new DbHelper();

        //根据编码查询部门信息
        public MisModel.Depart GetById(string depId)
        {
            if (string.IsNullOrEmpty(depId))
                return null;
            string sql = string.Format("select * from DEPART_TAB where DEP_ID='{0}'", depId);
            DataTable dt=  dbHelper.ExecuteDataTable(sql);
            MisModel.Depart entity = null;
            if (dt != null && dt.Rows.Count > 0){
                entity = new MisModel.Depart();
                entity.DepID = dt.Rows[0]["DEP_ID"].ToString();
                entity.DepName = dt.Rows[0]["DEPART"].ToString();
                entity.F1 = dt.Rows[0]["F1"].ToString();
                entity.N1 = decimal.Parse(dt.Rows[0]["N1"].ToString());
                entity.FisStart = dt.Rows[0]["FisStart"].ToString();
                entity.FSale = dt.Rows[0]["F_sale"].ToString();
            }
            return entity;
        }

        //查询部门列表
        public DataTable GetAll()
        {
            string sql = "select DEP_ID,ltrim(rtrim(DEPART)) as DEPART,F1,N1,FisStart,F_sale,(DEP_ID + '.' + DEPART + '.' + F1) as ItemShow  from DEPART_TAB";
            return dbHelper.ExecuteDataTable(sql);
        }

        //添加部门信息
        public int Add(MisModel.Depart obj)
        {
            if (string.IsNullOrEmpty(obj.F1.ToString()))
            {
                obj.F1 = "Y";
            }
            string sql = string.Format("insert into DEPART_TAB(DEP_ID,DEPART,F1,N1,FisStart,F_sale) values('{0}','{1}','{2}','{3}','{4}','{5}')", obj.DepID, obj.DepName, obj.F1,obj.N1,obj.FisStart,obj.FSale);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //修改部门信息
        public int Update(MisModel.Depart obj)
        {
            string sql = string.Format("update DEPART_TAB set DEPART='{0}',F1='{1}',N1='{2}',FisStart='{3}',F_sale='{4}' where DEP_ID='{5}'", obj.DepName, obj.F1, obj.N1, obj.FisStart, obj.FSale, obj.DepID);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //根据ID删除部门信息
        public int Delete(string id)
        {
            string sql = string.Format("delete from DEPART_TAB where DEP_ID='{0}'", id);
            return dbHelper.ExecuteNonQuery(sql);
        }
    }
}
